package com.fh.controller.information.photo;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.service.information.photo.PhotoService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.DateUtil;
import com.fh.util.PageData;

/**
 * 
 * @包名 :com.fh.controller.system.company
 * @文件名 :CompanyController.java
 *      TODO 类作用：相册
 * @Author: 孙超文 386480250@qq.com
 * @Date: 2015-7-2 下午5:37:33
 */
@Controller
@RequestMapping(value = "/photo")
public class PhotoController extends BaseController {
	@Resource(name = "PhotoService")
	private PhotoService photoService;

	/**
	 * 
	 * TODO 方法作用：进入公共相册列表
	 * 
	 * @param session
	 * @param page
	 * @return
	 * @throws Exception
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:42:11
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpSession session, Page page) throws Exception {
		logBefore(logger, "相册列表");
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();

			// 检索条件================================
			String name = pd.getString("name");
			if ((null != name) && !"".equals(name)) {
				name = name.trim();
				pd.put("name", name);
			}
			// 检索条件================================
			// 公共相册类型0，个人 1
			pd.put("type", "0");
			pd.put("recordid", this.getSessionCompanyid());
			page.setPd(pd);
			List<PageData> varList = photoService.list(page);
			mv.setViewName("information/photo/photo_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX, this.getHC()); // 按钮权限
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至详细页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:45:53
	 */
	@RequestMapping(value = "/goFind")
	public ModelAndView goFind() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = photoService.findById(pd);
			mv.setViewName("information/photo/photo_from");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至新增页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:42:55
	 */
	@RequestMapping(value = "/goAdd")
	public ModelAndView goAdd() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("information/photo/photo_edit");
			mv.addObject("msg", "save");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	@RequestMapping(value = "/save")
	public ModelAndView save(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("id", get32UUID());
		pd.put("type", "0");
		pd.put("recordid", this.getSessionCompanyid());
		pd.put("creator", getSessionUserid()); // 新增人
		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("createdate", DateUtil.getTime()); // 新增时间
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		photoService.save(pd);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：跳转至修改页面
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:45:53
	 */
	@RequestMapping(value = "/goEdit")
	public ModelAndView goEdit() {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			pd = photoService.findById(pd);
			mv.setViewName("information/photo/photo_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 
	 * TODO 方法作用：修改相册信息
	 * 
	 * @param out
	 * @return
	 * @throws Exception
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:46:33
	 */
	@RequestMapping(value = "/edit")
	public ModelAndView edit(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("modifier", getSessionUserid()); // 修改人
		pd.put("modifydate", DateUtil.getTime()); // 修改时间
		photoService.edit(pd);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}

	/**
	 * 物理删除
	 */
	@RequestMapping(value = "/delete")
	public void delete(PrintWriter out) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		try {
			pd = this.getPageData();
			photoService.delete(pd);
			out.write("success");
			out.close();
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}

	}

	/**
	 * 批量删除
	 */
	@RequestMapping(value = "/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除Pictures");
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if ((null != DATA_IDS) && !"".equals(DATA_IDS)) {
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				photoService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			} else {
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}

	/**
	 * 
	 * TODO 方法作用：权限操作
	 * 
	 * @return
	 * @Author: 孙超文
	 * @Date: 2015-7-2 上午9:19:32
	 */
	/* ===============================权限================================== */
	public Map<String, String> getHC() {
		Subject currentUser = SecurityUtils.getSubject(); // shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>) session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
}
